<?php
/**
*@作者:MissZhang
*@邮箱:<787727147@qq.com>
*@创建时间:2022/10/16 20:48
*@说明:交易控制器
*/
namespace app\admin\controller;


use app\common\model\Users;
use app\common\model\UserWallet;
use app\common\model\UserWalletDeal;
use app\common\model\UserWalletGiven;
use app\common\model\UserWalletLog;
use app\common\model\Wallet;
use app\common\util\AdminException;
use think\App;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Deal extends Base
{
    //交易列表
    public function user_wallet_deal_list(){
        // 搜索条件
        $where = [];
        $pay_code= input('pay_code');
        $status= input('status');
        $keyword = input('keyword');
        if ($pay_code){
            $where[]=['pay_code','=',$pay_code];
        }
        if ($status){
            if ($status==1){
                $where[]=['status','in','1,2'];
            }else{
                $where[]=['status','=',$status];
            }
        }
        if ($keyword){
            $ids=Db::name('users')->whereLike('mobile',"%$keyword%")->column('user_id');
            $where[]=['user_id|d_user_id','in',$ids];
        }
        $list = UserWalletDeal::where($where)
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->order('id desc')
            ->paginate([
                'query'=>['pay_code'=>$pay_code,'keyword'=>$keyword,'status'=>$status],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //会员某票列表
    public function user_wallet_list(){
        // 搜索条件
        $where = [];
        $search_type = input('search_type');
        $search_key = input('search_key');
        $wid = input('wid');
        if ($wid){
            $where[]=['wid','=',$wid];
        }
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }
        }
        $list = UserWallet::where($where)
            ->order('id desc')
            ->paginate([
                'query'=>['search_type'=>$search_type,'search_key'=>$search_key,'wid'=>$wid],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        $wallet_list=Wallet::field("id,name")->select();
        View::assign('list', $list);
        View::assign('page', $page);
        View::assign('wallet_list', $wallet_list);
        return view();
    }
    public function add_user_wallet(){
        $info=[];
        //这里的id是user_wallet表
        $id=input('id');
        if ($id){
            $info=UserWallet::find($id);
            if (empty($info)){
                $this->error('会员某票不存在');
            }
        }
        if (IS_POST){
            $data=input('post.');
            if ($info){
                $scene="edit";
            }else{
                $scene="add";
            }
            try {
                validate(\app\admin\validate\AddUserWallet::class)->scene($scene)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                $result['result']=$error;
                throw new AdminException($values[0],10,$result);
            }
            if ($info){
                $user=Users::where('user_id',$info['user_id'])->find();
            }else{
                $user=Users::where('mobile',$data['mobile'])->find();
            }
            $money=$data['money'];
            $desc=$data['desc'];
            if ($info){
                $wallet=Wallet::find($info['wid']);
                $res=user_wallet_log($user['user_id'],$id,$money,$desc?:'管理调整');
                //插入充值记录表
                inset_recharge_log($user['user_id'],0,$info['wid'],$money,$desc?:'管理调整');
                $mobile=$info['user']['mobile'];
            }else{
                $wallet=Wallet::find($data['wid']);
                $wallet_id=$wallet['id'];
                $res=user_wallet_log($user['user_id'],0,$money,$desc?:'管理调整',0,$wallet_id);
                //插入充值记录表
                inset_recharge_log($user['user_id'],0,$data['wid'],$money,$desc?:'管理调整');
                $mobile=$data['mobile'];
            }
            if ($res){
                adminLog("调节会员{$mobile}{$wallet['name']}");
                $data['url']=url('Deal/user_wallet_list')->build();
                throw new AdminException('操作成功',1,$data);
            }else{
                throw new AdminException('操作失败');
            }
        }
        $wallet=Wallet::where('1=1')->column('id,name');
        View::assign('wallet', $wallet);
        View::assign('info', $info);
        return view();
    }
    //会员某票记录列表
    public function user_wallet_log(){
        // 搜索条件
        $where = [];
        $id= input('id');
        if ($id){
            $where[]=['u_wid','=',$id];
        }
        $list = UserWalletLog::where($where)
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->order('id desc')
            ->paginate([
                'query'=>['id'=>$id],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //会员转让
    public function user_wallet_given_list(){
        // 搜索条件
        $where = [];
        $search_type = input('search_type');
        $search_key = input('search_key');
        $wid = input('wid');
        if ($wid){
            $where[]=['wid','=',$wid];
        }
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id|d_user_id','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id|d_user_id','in',$ids];
            }
        }
        $list = UserWalletGiven::where($where)
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->order('id desc')
            ->paginate([
                'query'=>['search_type'=>$search_type,'search_key'=>$search_key,'wid'=>$wid],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        $wallet_list=Wallet::field("id,name")->select();
        View::assign('list', $list);
        View::assign('page', $page);
        View::assign('wallet_list', $wallet_list);
        return view();
    }
}
